//
//  NSDate+YHCommon.m
//  YHBaseProject
//
//  Created by 刘宇航 on 2018/5/1.
//  Copyright © 2018 lyh. All rights reserved.
//
#import "NSDate+XMCommon.h"


@implementation NSDate (XMCommon)

#define kMinuteTimeInterval (60)
#define kHourTimeInterval (60 * kMinuteTimeInterval)
#define kDayTimeInterval (24 * kHourTimeInterval)
#define kWeekTimeInterval (7 * kDayTimeInterval)
#define kMonthTimeInterval (30 * kDayTimeInterval)
#define kYearTimeInterval (12 * kMonthTimeInterval)

- (BOOL)xm_isToday {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if ([today isEqualToDate:otherDate]) {
        return YES;
    }
    return NO;
}

- (BOOL)xm_isYesterday {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    NSDate *yesterday = [today dateByAddingTimeInterval:-kDayTimeInterval];
    
    components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if ([yesterday isEqualToDate:otherDate]) {
        return YES;
    }
    return NO;
}

- (NSString *)xm_shortTimeTextOfDate {
    NSDate *date = self;
    
    NSTimeInterval interval = [date timeIntervalSinceDate:[NSDate date]];
    
    interval = -interval;
    
    if ([date xm_isToday]) {
        // 今天的消息
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [dateFormat setAMSymbol:@"上午"];
        [dateFormat setPMSymbol:@"下午"];
        NSString *dateString = [dateFormat stringFromDate:date];
        return dateString;
    } else if ([date xm_isYesterday]) {
        // 昨天
        return @"昨天";
    } else if (interval < kWeekTimeInterval) {
        // 最近一周
        // 实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        [dateFormat setDateFormat:@"ccc"];
        NSString *dateString = [dateFormat stringFromDate:date];
        return dateString;
    } else {
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
        
        NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
        
        if ([components year] == [today year]) {
            // 今年
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            
            [dateFormat setDateFormat:@"MM-dd"];
            NSString *dateString = [dateFormat stringFromDate:date];
            return dateString;
        } else {
            // 往年
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yy-MM-dd"];
            NSString *dateString = [dateFormat stringFromDate:date];
            return dateString;
        }
    }
    return nil;
}

- (NSString *)xm_timeTextOfDate {
    NSDate *date = self;
    
    NSTimeInterval interval = [date timeIntervalSinceDate:[NSDate date]];
    
    interval = -interval;
    
    // 今天的消息
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"aHH:mm"];
    [dateFormat setAMSymbol:@"上午"];
    [dateFormat setPMSymbol:@"下午"];
    NSString *dateString = [dateFormat stringFromDate:date];
    
    if ([date xm_isToday]) {
        // 今天的消息
        return dateString;
    } else if ([date xm_isYesterday]) {
        // 昨天
        return [NSString stringWithFormat:@"昨天 %@", dateString];
    } else if (interval < kWeekTimeInterval) {
        // 最近一周
        // 实例化一个NSDateFormatter对象
        NSDateFormatter *weekFor = [[NSDateFormatter alloc] init];
        weekFor.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        [weekFor setDateFormat:@"ccc"];
        NSString *weekStr = [weekFor stringFromDate:date];
        return [NSString stringWithFormat:@"%@ %@", weekStr, dateString];
    } else {
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
        
        NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
        
        if ([components year] == [today year]) {
            // 今年
            NSDateFormatter *mdFor = [[NSDateFormatter alloc] init];
            [mdFor setDateFormat:@"MM-dd"];
            NSString *mdStr = [mdFor stringFromDate:date];
            return [NSString stringWithFormat:@"%@ %@", mdStr, dateString];
        } else {
            // 往年
            NSDateFormatter *ymdFormat = [[NSDateFormatter alloc] init];
            [ymdFormat setDateFormat:@"yy-MM-dd"];
            NSString *ymdString = [ymdFormat stringFromDate:date];
            return [NSString stringWithFormat:@"%@ %@", ymdString, dateString];
            ;
        }
    }
    return nil;
}

+ (NSString *)xm_stringCurrentDateWithDateFormat:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [self xm_dateFormatter:dateFormat];
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSString *)xm_stringFromDate:(NSDate *)date dateFormat:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [self xm_dateFormatter:dateFormat];
    return [dateFormatter stringFromDate:date];
}

+ (NSDate *)dateFromDateString:(NSString *)dateString dateFormat:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [self xm_dateFormatter:dateFormat];
    return [dateFormatter dateFromString:dateString];
}

+ (NSString *)stringTransDateFormatWithDateStr:(NSString *)dateStr oldDateFormat:(NSString *)oldDateFormat newDateFormat:(NSString *)newDateFormat {
    NSDate *date = [NSDate dateFromDateString:dateStr dateFormat:oldDateFormat];
    return [NSDate xm_stringFromDate:date dateFormat:newDateFormat];
}

+ (NSDateComponents *)xm_lunarCalendarWithDate:(NSDate *)date {
    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
    return localeComp;
}

+ (NSInteger)compareDateA:(NSString *)dateA dateB:(NSString *)dateB dateFormatter:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [self xm_dateFormatter:dateFormat];
    
    NSDate *dta = [dateFormatter dateFromString:dateA];
    NSDate *dtb = [dateFormatter dateFromString:dateB];
    NSComparisonResult result = [dta compare:dtb];
    NSInteger aa = 0;
    if (result == NSOrderedSame) { //相等
        aa = 0;
    } else if (result == NSOrderedAscending) { //上升
        aa = 1;
    } else if (result == NSOrderedDescending) { //下降
        aa = -1;
    }
    return aa;
}

+ (NSComparisonResult)xm_compareDateWithDateA:(NSString *)dateA dateB:(NSString *)dateB dateFormatter:(NSString *)dateFormatter {
    NSDateFormatter *df = [self xm_dateFormatter:dateFormatter];
    NSDate *dta = [df dateFromString:dateA];
    NSDate *dtb = [df dateFromString:dateB];
    NSComparisonResult result = [dta compare:dtb];
    return result;
}

+ (BOOL)isEqual:(NSDate *)dateA other:(NSDate *)dateB dateFormatter:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [self xm_dateFormatter:dateFormat];
    return [[dateFormatter stringFromDate:dateA] isEqualToString:[dateFormatter stringFromDate:dateB]];
}

+ (BOOL)isCurrentDate:(NSDate *)date dateFormatter:(NSString *)dateFormat {
    return [NSDate isEqual:[NSDate date] other:date dateFormatter:dateFormat];
}

+ (BOOL)xm_isToday:(NSDate *)date {
    return [NSDate isCurrentDate:date dateFormatter:@"yyyy-MM-dd"];
}

+ (NSInteger)xm_monthFromDate:(NSDate *)date {
    NSDateComponents *comps = [[NSDate xm_calendar] components:NSCalendarUnitMonth fromDate:date];
    return comps.month;
}

+ (NSInteger)xm_weekInMonthIndexWithDate:(NSDate *)date {
    NSDateComponents *components = [[NSDate xm_calendar] components:NSCalendarUnitWeekOfMonth fromDate:date];
    return components.weekOfMonth;
}

+ (NSInteger)xm_numberWeeksInMonthWithDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; //指定日历的算法
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth inUnit:NSCalendarUnitMonth forDate:date];
    return range.length;
}

+ (NSDate *)xm_datePeriodOfDateFromCurrentDateWithComponentsType:(YHDateComponentsType)type periodLength:(NSInteger)length {
    return [self xm_datePeriodOfDateFromStertDate:[NSDate date] componentsType:type periodLength:length];
}

+ (NSDate *)xm_datePeriodOfDateFromStertDate:(NSDate *)startDate
                              componentsType:(YHDateComponentsType)type
                                periodLength:(NSInteger)length {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    switch (type) {
        case YHDateComponentsTypeYear:
            components.year = length;
            break;
        case YHDateComponentsTypeMonth:
            components.month = length;
            break;
        case YHDateComponentsTypeDay:
            components.day = length;
            break;
        case YHDateComponentsTypeHour:
            components.hour = length;
            break;
        case YHDateComponentsTypeMinute:
            components.minute = length;
            break;
        case YHDateComponentsTypeSecond:
            components.second = length;
            break;
        case YHDateComponentsTypeWeekday:
            components.weekday = length;
            break;
        default:
            break;
    }
    
    NSDate *nextData = [[self xm_calendar] dateByAddingComponents:components toDate:startDate options:NSCalendarMatchStrictly];
    return nextData;
}


+ (NSInteger)weekDayStringWithDate:(NSDate *)date {
    NSCalendar *calendar = [self xm_calendar]; // 指定日历的算法
    NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:date];
    // 1 是周日，2是周一 3.以此类推
    return [comps weekday];
}

+ (BOOL)isWorkingDayWith:(NSDate *)date {
    NSInteger index = [self weekDayStringWithDate:date];
    if (index == 1 || index == 7) {
        return NO;
    }
    return YES;
}

#pragma mark - 通用
+ (NSCalendar *)xm_calendar {
    static NSCalendar *calendar;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        calendar.timeZone = [NSTimeZone localTimeZone];
    });
    return calendar;
}

+ (NSCalendar *)xm_chineseCalendar {
    static NSCalendar *calendar;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
        calendar.timeZone = [NSTimeZone localTimeZone];
    });
    return calendar;
}

+ (NSDateFormatter *)xm_dateFormatter:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dateFormat;
    [dateFormatter setCalendar:[NSDate xm_calendar]];
    dateFormatter.timeZone = [NSDate xm_calendar].timeZone;
    return dateFormatter;
}

+ (NSString *)xm_getCurrentTimeIntervalWithLenght:(NSInteger)lenght {
    // 获取当前时间0秒后的时间
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    // *1000 是精确到毫秒(13位),不乘就是精确到秒(10位)
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    if (lenght == 13) {
        timeInterval = timeInterval * 1000;
    }
    NSString *timeString = [NSString stringWithFormat:@"%.0f", timeInterval];
    return timeString;
}

+ (NSDateComponents *)xm_dateComponentsWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSDate xm_calendar]; //当前用户的calendar
    NSDateComponents *components = [calendar components:
                                    NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitNanosecond | NSCalendarUnitCalendar | NSCalendarUnitTimeZone | NSCalendarUnitYearForWeekOfYear | NSCalendarUnitQuarter
                                               fromDate:date];
    return components;
}

- (NSString *)getDateDisplayString:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    NSDateComponents *myCmps = [calendar components:unit fromDate:date];
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:date];
    
    if (nowCmps.year != myCmps.year) {
        dateFmt.dateFormat = @"yyyy/MM/dd";
    } else {
        if (nowCmps.day == myCmps.day) {
            dateFmt.AMSymbol = @"上午";
            dateFmt.PMSymbol = @"下午";
            dateFmt.dateFormat = @"aaa hh:mm";
        } else if ((nowCmps.day - myCmps.day) == 1) {
            dateFmt.AMSymbol = @"上午";
            dateFmt.PMSymbol = @"下午";
            dateFmt.dateFormat = @"昨天";
        } else {
            if ((nowCmps.day - myCmps.day) <= 7) {
                switch (comp.weekday) {
                    case 1:
                        dateFmt.dateFormat = @"星期日";
                        break;
                    case 2:
                        dateFmt.dateFormat = @"星期一";
                        break;
                    case 3:
                        dateFmt.dateFormat = @"星期二";
                        break;
                    case 4:
                        dateFmt.dateFormat = @"星期三";
                        break;
                    case 5:
                        dateFmt.dateFormat = @"星期四";
                        break;
                    case 6:
                        dateFmt.dateFormat = @"星期五";
                        break;
                    case 7:
                        dateFmt.dateFormat = @"星期六";
                        break;
                    default:
                        break;
                }
            } else {
                dateFmt.dateFormat = @"yyyy/MM/dd";
            }
        }
    }
    return [dateFmt stringFromDate:date];
}

@end
